package hw2.jpa.spring.service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import hw2.jpa.spring.model.Contract;
import hw2.jpa.spring.model.Product;
import hw2.jpa.spring.service.iface.RevenueRecognizer;

@Service
public class RevenueRecognizerImpl implements RevenueRecognizer {

	@PersistenceContext
	private EntityManager entityManager;

	@Transactional
	public long createProduct(Product product) {
		entityManager.persist(product);
		entityManager.flush();
		return product.getId();
	}

	@Transactional(readOnly = true)
	public Product findProduct(Long id) {
		return (Product) entityManager.find(Product.class, id);
	}

	@Transactional
	public long createContract(Contract contract) {
		entityManager.persist(contract);
		entityManager.flush();
		return contract.getId();
	}

	@Transactional(readOnly = true)
	public Contract findContract(Long id) {
		return (Contract) entityManager.find(Contract.class, id);
	}

}
